package org.example.member.service.impl;

import org.example.core.constant.TokenConstants;
import org.example.core.utils.R;
import org.example.member.constant.WareRedisConstants;
import org.example.member.dao.MemberCollectSkuDao;
import org.example.redis.service.RedisService;
import org.example.security.handler.SecurityHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.member.service.MemberCollectSkuService;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;


@Service("memberCollectSkuService")
public class MemberCollectSkuServiceImpl implements MemberCollectSkuService {

    @Autowired
    private MemberCollectSkuDao memberCollectSkuDao;
    @Autowired
    private RedisService redisService;

    /**
     * 添加收藏
     * @param spuId
     * @return
     */
    @Override
    public R<?> addCollect(Long spuId) {
        Long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        boolean existSet = redisService.isExistSet(WareRedisConstants.MEMBER_COLLECT + memberId, spuId);
        // 已收藏
        if (existSet){
            return R.ok();
        }
        // 数据库存在
        Long collectId = memberCollectSkuDao.getIsCollect(spuId, memberId);
        if (collectId != null){
            redisService.setSetObject(WareRedisConstants.MEMBER_COLLECT + memberId, spuId);
            return R.ok();
        }
        LocalDateTime dateTime = LocalDateTime.now();
        int saveCollect = memberCollectSkuDao.saveCollect(spuId, memberId, dateTime);
        if (saveCollect > 0){
            redisService.setSetObject(WareRedisConstants.MEMBER_COLLECT + memberId, spuId);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 判断用户是否收藏了该商品
     * @return
     */
    @Override
    public boolean isCollect(Long spuId) {
        Long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        boolean existSet = redisService.isExistSet(WareRedisConstants.MEMBER_COLLECT + memberId, spuId);
        if (existSet){
            return true;
        }else {
            Long collectId = memberCollectSkuDao.getIsCollect(spuId, memberId);
            if (collectId != null){
                redisService.setSetObject(WareRedisConstants.MEMBER_COLLECT + memberId, spuId);
                return true;
            }
            return false;
        }
    }

    /**
     * 删除收藏
     * @param spuIds
     */
    @Override
    public void delete(Long[] spuIds) {
        Long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        // 删数据库
        int deleteCollect = memberCollectSkuDao.deleteCollect(spuIds, memberId);
        if (deleteCollect > 0){
            // 删缓存
            redisService.deleteSetObject(WareRedisConstants.MEMBER_COLLECT + memberId, spuIds);
        }
    }

    /**
     * 查询收藏商品详情
     */
    @Override
    public List<Long> queryCollectSpuIds() {
        String member = SecurityHandler.getData().get(TokenConstants.MEMBER_ID);
        Long memberId = Long.parseLong(member);
        List<Long> spuIds = memberCollectSkuDao.getCollectInfo(memberId);

        return spuIds;
    }
}